﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict.DataModel;

namespace Room
{
    public class RCAIGuard : RCAIUnionFightBase
    {
        #region 数据
        public InstUFGuard instData;
        public TableUnionFightGuard dictData;
        #endregion

        public RCAIGuard(RoomCharactor owner)
            :base(owner)
        {
            this.type = AIType.UnionGuard;
        }

        public void Init(InstUFGuard instData)
        {
            UpdateData(instData);

            InitFSM();
        }

        
        public void UpdateData(InstUFGuard instData)
        {
            this.instData = instData;
            this.dictData = Dict.Blo.DictUnionFightGuardBlo.GetByID(instData.UFGuardId+"");
        }


        #region FSM
        private UnionGuardState currState = UnionGuardState.None;
        public UnionGuardState CurrState { get { return currState; } }
        private Dictionary<UnionGuardState, RCAIGuardBase> allState = new Dictionary<UnionGuardState, RCAIGuardBase>();

        public RCAIGuardBase currentState;
        public override void Tick()
        {
            base.Tick();
            //if (currState != UnionGuardState.None)
            //    allState[currState].Tick();
            if (currentState != null)
                currentState.Tick();
        }

        private void InitFSM()
        {
            allState[UnionGuardState.None] = new RCAIGuardBase();
            allState[UnionGuardState.Stand] = new RCAIGuardStand();
            allState[UnionGuardState.Attack] = new RCAIGuardAttack();
            allState[UnionGuardState.Death] = new RCAIGuardDeath();
            allState[UnionGuardState.Move] = new RCAIGuardMove();


            foreach (var state in allState)
            {
                state.Value.controller = this;
            }
            SwitchState((int)UnionGuardState.Stand);
        }

        public override void SwitchState(int newState)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionGuardState)newState;
                allState[currState].Enter();
                currentState = allState[currState];
            }
        }

        public override void SwitchState(int newState, object param)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionGuardState)newState;
                allState[currState].Enter(param);

                currentState = allState[currState];
            }
        }

        #endregion

    }
}